﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERP.DAL;
using ERP.DAL.Util;
using ERP.BusinessEntities.Util;
using DevExpress.XtraEditors;
using ERP.DAL.Base;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using ERP.BusinessEntities.Factory;
using ERP.DataAccess.Factory;

namespace ERP.Studio.Generator
{
    public partial class BusinessFactoryForm : Form
    {
        BindingList<Table> bdListTable = new BindingList<Table>();
        public IStoreFactory StoreFactory;
        public Host host;
        public BusinessFactoryForm()
        {
            InitializeComponent();
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("Test");
            host = new Host(db);
            textBox1.Text = Application.StartupPath + "\\Temp";
            StoreFactory = new SQLServer_StoreFactory();

            DevExpress.XtraEditors.TextEdit tx = new TextEdit();
            //tx.Properties.DisplayFormat = new DevExpress.Utils.FormatInfo(
            //tx.Properties.Mask  = new DevExpress.XtraEditors.Mask.MaskProperties(
        }

        public void InitGridView()
        {
            gridView1.OptionsBehavior.Editable = true;
            gridView1.OptionsView.ShowGroupPanel = false;

            gridView1.Columns[1].OptionsColumn.AllowEdit = true;
            gridView1.Columns[0].Caption = "A";
            gridView1.BestFitColumns();
        }

        private void GetAllTable_Click(object sender, EventArgs e)
        {
            IDBUtil dbUtil = new SQLDBUtil();
            List<String> lstTableNames = dbUtil.GetListTableInDatabase();

            bdListTable.Clear();
            foreach (String strTableName in lstTableNames)
            {
                bdListTable.Add(new Table(strTableName, false));
            }
            gridControl1.DataSource = bdListTable;


            InitGridView();
        }

        private void GenInfo_Click(object sender, EventArgs e)
        {
            String strFolderPath = textBox1.Text;
            tabControl1.TabPages.Clear();
            foreach (Table table in bdListTable)
            {
                if (table.Selected == false)
                    continue;

                String strInfo = BusinessFactory.GenerateBusinessInfo(table.TableName, strFolderPath);
                ShowTextOnTabCtrl(table.TableName, strInfo);
                //iCount++;
            }
        }

        public void ShowTextOnTabCtrl(String TabName, String strText)
        {
            tabControl1.TabPages.Add(TabName, TabName);
            RichTextBox txtBox = new RichTextBox();
            //txtBox.Properties.AutoHeight = false;
            txtBox.Text = strText;
            tabControl1.TabPages[TabName].Controls.Add(txtBox);
            txtBox.Dock = DockStyle.Fill;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDlg = new FolderBrowserDialog();
            folderDlg.ShowDialog();
            String strFolderPath = folderDlg.SelectedPath;
            textBox1.Text = strFolderPath;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            String strFolderPath = textBox1.Text;
            tabControl1.TabPages.Clear();
            foreach (Table table in bdListTable)
            {
                if (table.Selected == false)
                    continue;
                String strCtrl = BusinessFactory.GenerateBusinessCtrl(table.TableName, strFolderPath);
                AddTab(table.TableName, strCtrl);
            }
        }

        private void AddTab(String Tabname, String strText)
        {
            tabControl1.TabPages.Add(Tabname, Tabname);
            RichTextBox txtBox = new RichTextBox();
            //txtBox.Properties.AutoHeight = false;
            txtBox.Text = strText;
            tabControl1.TabPages[Tabname].Controls.Add(txtBox);
            txtBox.Dock = DockStyle.Fill;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            String strFolderPath = textBox1.Text;
            String strOpen = strFolderPath;
            if (String.IsNullOrEmpty(strFolderPath))
            {
                strOpen = Application.StartupPath;
            }
            try
            {
                System.Diagnostics.Process.Start(strOpen);
            }
            catch (System.Exception ex)
            {
            	
            }
            
        }

        private void GenStore_Click(object sender, EventArgs e)
        {
            tabControl1.TabPages.Clear();
            foreach (Table table in bdListTable)
            {
                if (table.Selected == false)
                    continue;
                StoreFactory.InitData(Host.sqlDbUtil, table.TableName);
                StringBuilder strBuild = new StringBuilder();
                String strLicence = "**************************************************************\n";
                strLicence += "Generated By:    Mr.Phuong\n";
                strLicence += "Date: " + DateTime.Now.ToShortDateString();
                strLicence += "\n**************************************************************\n\n\n";
                strBuild.Append(strLicence);

                String strInsert = StoreFactory.GenStore_Insert(table.TableName);
                strBuild.Append(strInsert);
                strBuild.Append("\n\n**************************************************************\n\n\n");
                Host.sqlDbUtil.CreateStroredProcedure(strInsert);

                String strUpdate = StoreFactory.GenStore_Update(table.TableName);
                strBuild.Append(strUpdate);
                strBuild.Append("\n\n**************************************************************\n\n\n");
                Host.sqlDbUtil.CreateStroredProcedure(strUpdate);

                String strSelectbyId = StoreFactory.GenStore_SelectById(table.TableName);
                strBuild.Append(strSelectbyId);
                strBuild.Append("\n\n**************************************************************\n\n\n");
                Host.sqlDbUtil.CreateStroredProcedure(strSelectbyId);

                String strSelectAll = StoreFactory.GenStore_SelectAll(table.TableName);
                strBuild.Append(strSelectAll);
                strBuild.Append("\n\n**************************************************************\n\n\n");
                Host.sqlDbUtil.CreateStroredProcedure(strSelectAll);

                String strDelete = StoreFactory.GenStore_Delete(table.TableName);
                strBuild.Append(strDelete);
                strBuild.Append("\n\n**************************************************************\n\n\n");
                Host.sqlDbUtil.CreateStroredProcedure(strDelete);


                String strdeleteAll = StoreFactory.GenStore_DeleteAll(table.TableName);
                strBuild.Append(strdeleteAll);
                strBuild.Append("\n\n**************************************************************\n\n\n");
                Host.sqlDbUtil.CreateStroredProcedure(strdeleteAll);

                AddTab(table.TableName, strBuild.ToString());
            }
        }

    }
}
